//
//  TBZXRootViewController.m
//  uplife
//
//  Created by ZhangCheng on 2018/12/28.
//  Copyright © 2018年 ZhangCheng. All rights reserved.
//

#import "TBZXRootViewController.h"
#import "UIImageView+WebCache.h"

@interface TBZXRootViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSArray *dataArray;
@property(nonatomic,weak)IBOutlet UITableView *tableView;
@end

@implementation TBZXRootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self sendRequestForLoadData];
}

-(void)sendRequestForLoadData{
    NSString *urlString = [NSString stringWithFormat:@"%@/product/list.do",JSXF_HOST];
    
    [SVProgressHUD show];
    
    [ZCWebServiceHelper requestWithMethod:GET andUrlStr:urlString andParameters:nil andFinished:^(id response, NSURLSessionDataTask *dataTask) {
        [SVProgressHUD dismiss];
        if (![response[@"data"] isKindOfClass:[NSString class]]) {
            NSDictionary *dic = response;
            self.dataArray = dic[@"data"][@"dataList"];
            [self.tableView reloadData];
        }
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    NSDictionary *dic = self.dataArray[indexPath.row];
    UIImageView *imageView = [cell viewWithTag:1];
    UILabel *nameLabel = [cell viewWithTag:2];
    UILabel *descLabel = [cell viewWithTag:3];
    UILabel *storeLabel = [cell viewWithTag:4];
    UILabel *priceLabel = [cell viewWithTag:5];
    UILabel *originPriceLabel = [cell viewWithTag:6];
    
    [imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",JSXF_HOST_FILE,dic[@"pic"]]]];
    nameLabel.text = [NSString stringWithFormat:@"%@",dic[@"name"]];
    NSString *descString =[NSString stringWithFormat:@"%@",dic[@"intro"]];
    NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:descString];
    NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle1 setLineSpacing:8];
    [attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [descString length])];
    [descLabel setAttributedText:attributedString1];
    [descLabel sizeToFit];
    storeLabel.text = [NSString stringWithFormat:@"数量：仅剩余%@件",dic[@"stockQuantity"]];
    priceLabel.text = [NSString stringWithFormat:@"%@",FORMAT_PRICE(dic[@"currentPrice"])];
    
    [originPriceLabel setAttributedText:[[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"原价：¥%@",FORMAT_PRICE(dic[@"originalPrice"])] attributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle|NSUnderlinePatternSolid)}]];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = self.dataArray[indexPath.row];

    [self.fourthViewController performSegueWithIdentifier:@"toDetail" sender:dic[@"id"]];
}

-(IBAction)buyAction:(UIButton*)sender{
    UIView *testView1 = sender.superview;
    while([testView1 isKindOfClass:[UITableViewCell class]]==NO){
        testView1 = testView1.superview;
    }
    
    UITableViewCell *cell = (UITableViewCell *)testView1;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    NSDictionary *dic = self.dataArray[indexPath.row];

    [self.fourthViewController performSegueWithIdentifier:@"toBuy" sender:dic];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
